import { createRouter, createWebHistory } from 'vue-router'
import Index from '@/views/home/index.vue'
import { isLogin } from '@/utils'
import { useUserStore } from '@/stores'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      meta: {
        title: '首页'
      },
      component: Index,
    },
    {
      path: '/category',
      name: 'category',
      meta: {
        title: '分类'
      },
      component: () => import('@/views/category.vue'),
    },
    {
      path: '/goods',
      name: 'goods',
      meta: {
        title: '商品详情'
      },
      component: () => import('@/views/goods/index.vue'),
    },
    {
      path: '/shop',
      name: 'shop',
      meta: {
        title: '店铺名称'
      },
      component: () => import('@/views/shop/index.vue'),
    },
    {
      path: '/evaList',
      name: 'evaList',
      meta: {
        title: '评价列表'
      },
      component: () => import('@/views/evaList.vue'),
    },
    {
      path: '/collect',
      name: 'collect',
      meta: {
        title: '我的收藏'
      },
      component: () => import('@/views/collectList.vue'),
    },
    {
      path: '/mine',
      name: 'mine',
      meta: {
        title: '我的'
      },
      component: () => import('@/views/mine/index.vue'),
    },
    {
      path: '/order',
      name: 'order',
      meta: {
        title: '订单'
      },
      component: () => import('@/views/order/index.vue'),
    },
    {
      path: '/confirmOrder',
      name: 'confirmOrder',
      meta: {
        title: '确认订单'
      },
      component: () => import('@/views/order/confirmOrder.vue'),
    },
    {
      path: '/cart',
      name: 'cart',
      meta: {
        title: '购物车'
      },
      component: () => import('@/views/cart/index.vue'),
    },
    {
      path: '/about',
      name: 'about',
      meta: {
        title: '关于我们'
      },
      component: () => import('@/views/about.vue'),
    },
    {
      path: '/help',
      name: 'help',
      meta: {
        title: '帮助中心'
      },
      component: () => import('@/views/helpCenter.vue'),
    },
    {
      path: '/address',
      name: 'address',
      meta: {
        title: '我的地址'
      },
      component: () => import('@/views/address/index.vue'),
    },
    {
      path: '/gift',
      name: 'gift',
      meta: {
        title: '礼品列表'
      },
      component: () => import('@/views/gift/index.vue'),
    },
    {
      path: '/giftConfirmOrder',
      name: 'giftConfirmOrder',
      meta: {
        title: '礼品确认订单'
      },
      component: () => import('@/views/gift/giftConfirmOrder.vue'),
    },
    {
      path: '/giftOrderList',
      name: 'giftOrderList',
      meta: {
        title: '礼品订单列表'
      },
      component: () => import('@/views/gift/giftOrderList.vue'),
    },
    {
      path: '/giftOrderDetail',
      name: 'giftOrderDetail',
      meta: {
        title: '礼品订单详情'
      },
      component: () => import('@/views/gift/giftOrderDetail.vue'),
    },
    {
      path: '/giftViewExpress',
      name: 'giftViewExpress',
      meta: {
        title: '查看物流'
      },
      component: () => import('@/views/gift/giftViewExpress.vue'),
    },
    {
      path: '/wallet',
      name: 'wallet',
      meta: {
        title: '我的钱包'
      },
      component: () => import('@/views/mine/wallet.vue'),
    },
    {
      path: '/orderDetail',
      name: 'orderDetail',
      meta: {
        title: '订单详情'
      },
      component: () => import('@/views/order/orderDetail.vue'),
    },
    {
      path: '/success',
      name: 'success',
      meta: {
        title: '下单成功'
      },
      component: () => import('@/views/result/success.vue'),
    },
    {
      path: '/evaOrder',
      name: 'evaOrder',
      meta: {
        title: '评价订单'
      },
      component: () => import('@/views/order/evaOrder.vue'),
    },
    {
      path: '/refund',
      name: 'refund',
      meta: {
        title: '申请退款'
      },
      component: () => import('@/views/order/refund.vue'),
    },
    {
      path: '/coupon',
      name: 'coupon',
      meta: {
        title: '优惠券列表'
      },
      component: () => import('@/views/couponList.vue'),
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { top: 0 }
  },
})
router.beforeEach((to, from, next) => {
  const { loginToggle } = useUserStore()
  if (to.name === 'mine' || to.name === 'order' || to.name === 'cart' || to.name === 'wallet') {
    if (isLogin()) next()
    else loginToggle(true)
  } else {
    next()
  }
})
export default router
